"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _leancloudStorage = _interopRequireDefault(require("leancloud-storage"));

var _roomManager = _interopRequireDefault(require("./room-manager"));

var _logger = _interopRequireDefault(require("./logger"));

var _utils = require("./utils");

_leancloudStorage.default.init({
  appId: 'l1buxITuSPQL5MatNdwhtcm2-gzGzoHsz',
  appKey: 'LVpkrT2ioDeTL92xHsafKnRb'
});
/**
 *
 * @class WxRoom
 */


var WxRoom =
/*#__PURE__*/
function () {
  function WxRoom() {
    var _this = this;

    (0, _classCallCheck2.default)(this, WxRoom);
    (0, _defineProperty2.default)(this, "hasRoom",
    /*#__PURE__*/
    function () {
      var _ref = (0, _asyncToGenerator2.default)(
      /*#__PURE__*/
      _regenerator.default.mark(function _callee(groupInfo) {
        var rooms;
        return _regenerator.default.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                _context.next = 2;
                return _this.wechatyManager.findRooms(groupInfo.topic);

              case 2:
                rooms = _context.sent;
                return _context.abrupt("return", rooms.some(function (room) {
                  return groupInfo.topic === room.topic();
                }));

              case 4:
              case "end":
                return _context.stop();
            }
          }
        }, _callee, this);
      }));

      return function (_x) {
        return _ref.apply(this, arguments);
      };
    }());
    // excuteIntervalRightNow(() => {
    //   const qrcodeQuery = new AV.Query('qrcode_info');
    //   qrcodeQuery.equalTo('status', 'reviewing');
    //   qrcodeQuery
    //   .find()
    //   .then(result => {
    //     this.validGroup(result.map(value => ({
    //       id: value.get('objectId'),
    //       topic: value.get('name'),
    //       owner: value.get('owner'),
    //     })));
    //   });
    // }, 1000*60*15);
    this.wechatyManager = new _roomManager.default();
  }

  (0, _createClass2.default)(WxRoom, [{
    key: "validGroup",
    value: function validGroup(groupInfos) {
      var _this2 = this;

      _logger.default.info('groupInfos %s', JSON.stringify(groupInfos));

      groupInfos.map(function (groupInfo) {
        (0, _utils.intervalTimes)(10, _this2.hasRoom, 1000 * 30, groupInfo).then(function (res) {
          _leancloudStorage.default.Query.doCloudQuery("update qrcode_info set status=\"success\" where objectId=\"".concat(res.id, "\""));
        }).catch(function (res) {
          _leancloudStorage.default.Query.doCloudQuery("update qrcode_info set status=\"failed\" where objectId=\"".concat(res.id, "\""));

          _this2.wechatyManager.saySelf("\u5BA1\u6838\u5931\u8D25\uFF1A".concat(JSON.stringify(res)));
        });
      });
    }
  }]);
  return WxRoom;
}();

(0, _asyncToGenerator2.default)(
/*#__PURE__*/
_regenerator.default.mark(function _callee2() {
  var wxRoom;
  return _regenerator.default.wrap(function _callee2$(_context2) {
    while (1) {
      switch (_context2.prev = _context2.next) {
        case 0:
          wxRoom = new WxRoom();
          _context2.next = 3;
          return wxRoom.wechatyManager.startBot();

        case 3:
        case "end":
          return _context2.stop();
      }
    }
  }, _callee2, this);
}))();